import {} from './hc-lang-editor.js'
import * as $rest from '/lib/hc-rest-service.js'

const hc$test = {
    data: function() {
        return {
            inputs_: '',
            outputs: '',
            returnCtx:false,
            status: -1,
            elapsed: 0
        }
    },
    props: ['rpath', 'inputs'],
    created: function() {

    },
    watch: {
        "inputs": {
            immediate: true,
            handler: function(nv, ov) {
                if (typeof(nv) === 'string') {
                    try {
                        nv = JSON.parse(nv)
                    } catch (ex) {
                        console.error('hc-test control', ex, nv)
                    }
                };
                this.inputs_ = typeof(nv) === 'string' ? nv : JSON.stringify(nv, null, '\t')
            },
        },
    },

    methods: {
        triggerSaveInputs: function() {
            this.$emit('save-inputs', this.inputs_)
            this.$message({
                message: '测试输入保存已被触发，保存后就能生效!',
                type: 'success'
            })
        },
        triggerDoTest: function() {
            var startAt = (new Date()).getTime();
            this.status = -1
            let testInput
            try {
                testInput = JSON.parse(this.inputs_);
            } catch (e) {
                this.outputs = e.message;
                this.elapsed = (new Date()).getTime() - startAt
                this.status = 1
                return;
            }
            let vm = this
            
            // $rest.post('/manage/run?rpath=' + this.rpath, testInput)
            $rest.post('/api/run', {rpath:this.rpath,data:testInput,returnCtx:this.returnCtx})
                .then(function(response) {
                    var ret = response.data;
                    vm.outputs = ret.code ? ret.data : JSON.stringify(ret.data, null, '\t');
                    vm.elapsed = (new Date()).getTime() - startAt
                    vm.status = ret.code ? 1 : 0;
                })
                .catch(function(error) {
                    vm.outputs = error;
                    vm.status = 1
                    vm.elapsed = (new Date()).getTime() - startAt
                });
        }
    },
    template: `
                <el-form label-width="80px" label-position='top'>
                	<el-form-item label="数据变量：">
                		<hc-lang-editor style='margin-top:-30px' ref='inputs' v-model="inputs_" placeholder="请提供JSON格式的输入变量"></hc-lang-editor>
                	</el-form-item>
                    <el-form-item style='margin-top:-30px'>
                        <el-checkbox type="checkbox" v-model="returnCtx" label="返回上下文变量"></el-checkbox><br/>
                		<el-button type="primary" @click='triggerDoTest()'>执行测试</el-button>
                		<el-link style="font-size: 12px" :underline="false" v-if="$listeners['save-inputs']" type="primary" @click='triggerSaveInputs'>保存输入下次使用</el-link>
                	</el-form-item>
                    <el-alert v-if='status == 0' :title="'执行成功，用时'+elapsed+'ms，以下是结果:'" type="success">
                    </el-alert>
                    <el-alert v-else-if='status == 1' :title="'执行失败，用时'+elapsed+'ms，以下是错误反馈:'" type="error">
                    </el-alert>
                	<el-form-item label="测试结果：">
                        <hc-lang-editor  style='margin-top:-30px' ref='outputs' v-model="outputs" :readonly='true'></hc-lang-editor>
                	</el-form-item>
                </el-form>
                `
}

Vue.component('hc-test', hc$test)
